package unm.app.wordjongg;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
//import android.view.Menu;
//import android.view.MenuInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

public class WordJongg extends BaseActivity implements OnClickListener {
   
	private static final int CAMERA_WIDTH = 320;
	private static final int CAMERA_HEIGHT = 480;
	
	private BoardView boardView;
	
	private static final String STATE_FILE_NAME = "state.data";

	// ===========================================================
	// Fields
	// ===========================================================

	private android.graphics.Camera mCamera;
	
	private static final String TAG = "WordJongg";
	
	public BoardView getBoardView() {
		return boardView;
	}
   
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.menu);
      
      LayoutProvider.init(this);
      Config.getInstance().init(this);
      //setContentView(R.layout.main);
      boardView = (BoardView)findViewById(R.id.boardView);

      // Set up click listeners for all the buttons
      View continueButton = findViewById(R.id.continue_button);
      continueButton.setOnClickListener(this);
      View newButton = findViewById(R.id.new_button);
      newButton.setOnClickListener(this);
      View aboutButton = findViewById(R.id.about_button);
      aboutButton.setOnClickListener(this);
      View exitButton = findViewById(R.id.exit_button);
      exitButton.setOnClickListener(this);
      
      boardView.setVisibility(View.GONE);
   }

   
   @Override
   protected void onResume() {
      super.onResume();
      //Music.play(this, R.raw.main);
   }

   @Override
   protected void onPause() {
      super.onPause();
      //Music.stop(this);
   }
   

   
   public void onClick(View v) {
      switch (v.getId()) {
      case R.id.continue_button:
         //startGame(Game.DIFFICULTY_CONTINUE);
    	  File file = getFileStreamPath(STATE_FILE_NAME);
  		if (file.exists()) {
  			try {
  				FileInputStream in = new FileInputStream(file);
  				try {
  					boardView.setVisibility(View.VISIBLE);
  					boardView.getController().loadState(in);

  				} finally {
  					in.close();
  				}
  			} catch (Exception e) {
  				Log.e("", e.getMessage(), e);
  				boardView.getController().startNewGame();
  			}
  		}
  		else
  			boardView.setVisibility(View.VISIBLE);
  			boardView.getController().startNewGame();
  			
         break;
         // ...
         
      case R.id.about_button:
         //Intent i = new Intent(this, About.class);
         //startActivity(i);
         break;
      // More buttons go here (if any) ...
      case R.id.new_button:
         //openNewGameDialog();
    	  boardView.setVisibility(View.VISIBLE);
    	  boardView.getController().startNewGame();
         break;
      case R.id.exit_button:
         finish();
         break;
         
      }
   }

   /** Ask the user what difficulty level they want */
   //private void openNewGameDialog() {
      //new AlertDialog.Builder(this)
           //.setTitle(R.string.new_game_title)
           //.setItems(R.array.difficulty,
            //new DialogInterface.OnClickListener() {
               //public void onClick(DialogInterface dialoginterface, int i) {
                  //startGame(i);
               //}
            //})
           //.show();
   //}

   /** Start a new game with the given difficulty level */
   private void startGame(int i) {
      Log.d(TAG, "clicked on " + i);
      //Intent intent = new Intent(WordJongg.this, Game.class);
      //intent.putExtra(Game.KEY_DIFFICULTY, i);
      //startActivity(intent);
      boardView = (BoardView)findViewById(R.id.boardView);
      boardView.getController().startNewGame();
   }
   
	@Override
	protected void onDestroy() {
		super.onDestroy();
		try {
		FileOutputStream out = openFileOutput(STATE_FILE_NAME, MODE_PRIVATE);
		try {
			boardView.getController().saveState(out);
		} finally {
			out.close();
		}
		} catch (Exception e) {
			Log.e("", e.getMessage(), e);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.new_game:
			boardView.getController().startNewGame();
			break;
		case R.id.restart:
			boardView.getController().restart();
			break;
		case R.id.undo:
			boardView.getController().undo();
			break;
		case R.id.hint:
			boardView.getController().showHint();
			break;
		case R.id.options:
			startActivityForResult(new Intent(Names.ACTION_OPTIONS), 1);
			break;
		case R.id.quit:
			finish();
			break;
		case R.id.about:
			String title = String.format("%1$s %2$s", 
					getResources().getString(R.string.app_name),
					getResources().getString(R.string.app_version));
			String message = String.format("%1$s\n%2$s", 
					getResources().getString(R.string.copyright),
					getResources().getString(R.string.url));
			boardView.showDialog(title, message);
			break;
		}
		return false;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode > 0)
			boardView.getController().startNewGame();
	}
   
}